<?php
class WishController{
	private $data;
	public function __construct(){
		$this->data = include './data.php';
	}
    public function add(){
        $data = include './data.php';
        //追加
        $data[] = $_POST;
        file_put_contents('./data.php', "<?php  return " . var_export($data,true) . "?>");
        //获得所有键名  取得最大的值
        $id = max(array_keys($data));
        //把post获得的数组转化为json形式 返回给异步；
        $_POST['id'] = $id;
        echo json_encode($_POST);
    }
    public function del(){
        $data = include './data.php';
        $bool = isset($_GET['id']);
        $id = intval($_GET['id']);
        if(!$bool) {
            echo json_encode('error');exit;
        }
        unset($data[$id]);
        file_put_contents('./data.php', "<?php\r\nreturn " . var_export($data , true) . "\r\n ?>");
        echo json_encode('success');exit;
    }
	public function edit(){
		$id = intval($_GET['id']);
		$bool = $this->check($id);
		if(!$bool) success('内容不存在', 'Index.php');
		$data = $this->data;
		$oldData = $data[$id];
		if(IS_POST){
			$_POST['sendtime'] = time();
			$this->data[] = $_POST;
			$data[$id] = $_POST;
			file_put_contents('./data.php',"<?php return ". var_export($data,true)."?>");
			success('修改成功', 'Index.php');
		}
		include 'view/edit.php';
	}
	private function check($id){
		if(!isset($this->data[$id])){
			return FALSE;
		}
		return true;
	}

}